﻿using System;
using System.Collections.Generic;

namespace Fuse8.DomainFramework.Common.Locator
{
    /// <summary>
    /// 
    /// </summary>
    public class RegistryServiceLocator : ServiceLocatorBaseEx
    {
        /// <summary>
        /// 
        /// </summary>
        public RegistryServiceLocator()
        {
            RegisterDependencyContainer.Instance.RegisterInstance<IServiceLocatorWithLazy>(this);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        protected override object DoGetInstance(Type serviceType, string key)
        {
            return RegisterDependencyContainer.Instance.Resolve(serviceType);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
        {
            return RegisterDependencyContainer.Instance.ResolveAll(serviceType);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="service"></param>
        public override void Register<TService>(TService service)
        {
            RegisterDependencyContainer.Instance.RegisterInstance<TService>(service);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="key"></param>
        /// <param name="service"></param>
        public override void Register<TService>(string key, TService service)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="typeFrom"></param>
        /// <param name="serviceType"></param>
        public override void Register(Type typeFrom, Type serviceType)
        {
            RegisterDependencyContainer.Instance.Register(typeFrom, serviceType);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="typeTo"></param>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        public override void Register(Type typeTo, Type serviceType, string key)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <returns></returns>
        public override bool IsRegistered<TService>()
        {
            return RegisterDependencyContainer.Instance.IsRegistered<TService>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public override bool IsRegistered<TService>(string key)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public override bool IsRegistered(Type serviceType)
        {
            return RegisterDependencyContainer.Instance.IsRegistered(serviceType);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public override bool IsRegistered(Type serviceType, string key)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="service"></param>
        public override void SetDefaultService<TService>(TService service)
        {
            RegisterDependencyContainer.Instance.SetDefaultService(service);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        public override void SetDefaultService(Type serviceType)
        {
            RegisterDependencyContainer.Instance.SetDefaultService(serviceType);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <returns></returns>
        public override TService GetDefaultService<TService>()
        {
            return RegisterDependencyContainer.Instance.GetDefaultService<TService>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public override object GetDefaultService(Type serviceType)
        {
            return RegisterDependencyContainer.Instance.GetDefaultService(serviceType);
        }

        /// <summary>
        /// Check if an instance of TService is registered in IoC as default service
        /// </summary>
        /// <typeparam name="TService">Type of instance</typeparam>
        /// <returns>True if registered, false if not</returns>
        public override bool IsDefaultServiceRegistered<TService>()
        {
            return RegisterDependencyContainer.Instance.IsRegistered<TService>();   
        }

        /// <summary>
        /// Check if a type is registered in IoC as a type for default service
        /// </summary>
        /// <param name="serviceType">Type of registration</param>
        /// <returns>True if registered, false if not</returns>
        public override bool IsDefaultServiceRegistered(Type serviceType)
        {
            return RegisterDependencyContainer.Instance.IsRegistered(serviceType);       
        }
    }
}
